// pages/hotrank/hotrank.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    screenHeight: app.globalData.screenHeight,
    tabs: ['院校热度', '专业热度'],
    tabsindex: 0,
    schoolHeatList: [],
    majorHeatList: [],
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    let schoolHeatList = wx.getStorageSync('schoolHeatList');
    let majorHeatList = wx.getStorageSync('majorHeatList');
    if(!majorHeatList.length){
      that.getMajorHeat(that);
    }else {
      that.setData({
        majorHeatList:majorHeatList
      })
    }
    if(!schoolHeatList.length){
      that.getSchoolHeat(that);
    }else{
      that.setData({
        schoolHeatList:schoolHeatList
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  tabsClick(e) {
    this.setData({
      tabsindex: e.currentTarget.dataset.index
    })
  },

  //获取院校热度排行
  getSchoolHeat(that) {
    app.globalData.wxRequest.requestGet('data/getSchoolHeat.do').then(res => {
      console.log(res);
      let schoolHeatList = res.data;

      if (res.code == '201') {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
        return
      }
      that.setData({
        schoolHeatList: schoolHeatList,
      }, function () {
        wx.setStorageSync('schoolHeatList', schoolHeatList)
      })
    }).catch(err => {
      wx.showToast({
        title: '请求失败',
        icon: 'error'
      })
    })
  },

  //获取专业热度排行
  getMajorHeat(that) {
    app.globalData.wxRequest.requestGet('data/getMajorHeat.do').then(res => {
      console.log(res);
      let majorHeatList = res.data;
      if (res.code == '201') {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
        return
      }
      that.setData({
        majorHeatList: majorHeatList,
      }, function () {
        wx.setStorageSync('majorHeatList', majorHeatList)
      })
    }).catch(err => {
      wx.showToast({
        title: '请求失败',
        icon: 'error'
      })
    })
  },
})